var config = require("../../config");
var QQMapWX = require('../../qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  data: {},
  onLoad() {
   
  },
  onShareAppMessage() {

  },
  form(e) {    //样式改变
    let _this = this,item = e.currentTarget.dataset.index;
    if (item == 2) {
      wx.chooseLocation({
        success: function (res) {
          console.log(res);
          _this.setData({
            regionValue: res.address
          });
        },
      });
    }
  },
  phoneNumberBlur(e) {   //验证手机号码
    var phoneNumber = e.detail.value;
    if (testPhoneNumber(phoneNumber) != true && phoneNumber.length > 0) {
      this.setData({
        NPfault: {   //未选择手机故障摸版该作为消息提示摸版
          hint_NPfault: 'hint-NPfault',
          info: '手机号格式不正确',
          buttonEvent: "buttonEvent"
        }
      });
    }
  },
  
  formSubmit(e) {    //提交表单  
    var formObj = e.detail.value,_this = this;
    if (formObj.name && formObj.phoneNumber && formObj.region != "") {
      
      if (testPhoneNumber(formObj.phoneNumber) == true) {

       wx.getNetworkType({
         success: function(res) {
           if(res.networkType != "none"){
             wx.showLoading({
               title: "添加中...",
               mask:true
             });
             wx.request({    //添加收货地址
               method: "POST",
               url: config.service.addAddress,
               data: {
                 uid: wx.getStorageSync("loginInfo").uid,
                 logintoken: wx.getStorageSync("loginInfo").token,
                 address_id: 1,
                 receive_name: formObj.name,
                 mobile: formObj.phoneNumber,
                 province: '',
                 city: '',
                 distance: '',
                 detail_addr: _this.data.regionValue+ formObj.site,
               },
               header: {
                 'content-type': 'application/x-www-form-urlencoded'
               },
               success(res) {
                 wx.hideLoading();
                 if (res.data.code == 1) {
                   wx.showModal({
                     title: '提示',
                     content: '添加成功,是否返回上一页',
                     success: function (e) {
                       if (e.confirm) {
                         wx.navigateBack();
                       }
                     }
                   });
                 } else {
                   wx.showToast({
                     title: res.data.msg,
                     icon: 'loading',
                     duration: 2000,
                     mask: true
                   });
                 }
               }
             });
           }else{
             wx.showToast({
               title: '好像没网了...',
               icon: 'loading',
               duration: 2000,
               mask: true
             });
           }
         },
       });
      }else{
        this.setData({
          NPfault: {   //未选择手机故障摸版该作为消息提示摸版
            hint_NPfault: 'hint-NPfault',
            info: '手机号格式不正确',
            buttonEvent: "buttonEvent"
          }
        });
      }
    } else if (formObj.name == ""){
      this.setData({
        NPfault: {   //未选择手机故障摸版该作为消息提示摸版
          hint_NPfault: 'hint-NPfault',
          info: '请填写收货人姓名',
          buttonEvent: "buttonEvent"
        }
      });
    } else if (formObj.phoneNumber == "") {
      this.setData({
        NPfault: {   //未选择手机故障摸版该作为消息提示摸版
          hint_NPfault: 'hint-NPfault',
          info: '请填写手机号',
          buttonEvent: "buttonEvent"
        }
      });
    } else if (formObj.region == "") {
      this.setData({
        NPfault: {   //未选择手机故障摸版该作为消息提示摸版
          hint_NPfault: 'hint-NPfault',
          info: '请填写地区信息',
          buttonEvent: "buttonEvent"
        }
      });
    } else if (formObj.site == "") {
      this.setData({
        NPfault: {   //未选择手机故障摸版该作为消息提示摸版
          hint_NPfault: 'hint-NPfault',
          info: '请填写详细地址',
          buttonEvent: "buttonEvent"
        }
      });
    }
  },
  buttonEvent() {    //确定未选择手机故障摸版该作为消息提示摸版  
    this.setData({
      NPfault: {
        hint_NPfault: ''
      }
    });
  },
});

function testPhoneNumber(value) {
  if (/^\d{11}$/.test(value)) {
    return true
  }
}